package com.rabbit.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfiguration {


    @Bean
    public Exchange exchange() {
        return new DirectExchange("e.order");
    }

    @Bean
    public Exchange exchangeDle() {
        return new DirectExchange("e.order.dle");
    }

    @Bean
    public Queue queue() {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("x-message-ttl", 1000*10);
        // 设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加 入死信队列）
        properties.put("x-dead-letter-exchange", "e.order.dle");
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原 队列的routingKey
        properties.put("x-dead-letter-routing-key", "r.order.dle");
        return new Queue("q.order", true, false, false, properties);
    }

    @Bean
    public Queue queueDle(){

        return new Queue("q.order.dle");
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(exchange()).with("r.order").noargs();
    }

    @Bean
    public Binding bindingDle(){
        return BindingBuilder.bind(queueDle()).to(exchangeDle()).with("r.order.dle").noargs();
    }

}
